package cn.xenosp.server.web.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.xenosp.server.common.annotation.LogInfo;
import cn.xenosp.server.common.system.TargetOperatingSystem;
import cn.xenosp.server.common.tools.ServiceUtil;
import cn.xenosp.server.module.third_server.entity.NginxEntity;
import cn.xenosp.server.module.third_server.service.NginxService;
import cn.xenosp.server.module.user.entity.SysUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 项目名： nginx-web
 * 包路径： cn.xenosp.nginx.web.controller
 * 作者：   TongHui
 * 创建时间: 2024-07-26 23:22
 * 描述: TODO
 * 版本: 1.0
 */
@Slf4j
@Controller
public class PageController {

    @Autowired
    TargetOperatingSystem operatingSystem;
    @Autowired
    NginxService nginxService;

    @Value("${user.username}")
    String username;
    @Value("${user.password}")
    String password;

    @GetMapping(value = {"/","${app.config.api-base-url}","/login"})
    public String login1(HttpServletRequest request, Model model) {
        model.addAttribute("message", request.getParameter("message"));
        return "login.html";
    }

    @LogInfo(operationModuleType = "视图模块",operationName = "主页",operationContent = "进入主页")
    @RequestMapping("${app.config.api-base-url}/index")
    public String index(HttpServletRequest request, Model model) {
//        boolean startServer = operatingSystem.isStartServer();
//        model.addAttribute("nginx", startServer);
        SysUser user = (SysUser)  StpUtil.getSession().get("user");
        model.addAttribute("user", user);
        return "index.html";
    }

    @LogInfo(operationModuleType = "视图模块",operationName = "访问页面",operationContent = "访问菜单页面")
    @PostMapping("${app.config.api-base-url}/webPageLoader")
    public String webPageLoader(@RequestBody Map<String,String> map, Model model){
        if (map == null || !map.containsKey("pageUrl")){
            throw new NullPointerException();
        }
        return map.get("pageUrl");
    }


    @RequestMapping("/from/{id}")
    public String from(@PathVariable("id")String id,  Model model) {
        if (ServiceUtil.isWINDOWS()){
            model.addAttribute("system", 0);
        }
        if (ServiceUtil.isLINUX()){
            model.addAttribute("system", 1);
        }

        if (id != null){
            model.addAttribute("nginx", nginxService.get(id));
        }

        return "/from/from.html";
    }

    @RequestMapping("/configFile/{id}")
    public String configFile(@PathVariable("id")String id,Model model) {
        try {
            NginxEntity nginxEntity = nginxService.get(id);
            if (ServiceUtil.isWINDOWS()){
                model.addAttribute("system", 0);
            }
            if (ServiceUtil.isLINUX()){
                model.addAttribute("system", 1);
            }
            model.addAttribute("nginx", nginxService.get(id));
            model.addAttribute("conf", operatingSystem.readConfig(nginxEntity.getConfigPath()));
            model.addAttribute("start", operatingSystem.readStart(nginxEntity));
            model.addAttribute("stop", operatingSystem.readStop(nginxEntity));
            model.addAttribute("restart", operatingSystem.readRestart(nginxEntity));
            return "/from/editConfig.html";
        } catch (IOException e) {
            e.printStackTrace();
            return "/error/5xx.html";
        }
    }

}
